<?php
/**
 * 
 * @property string $username 用户名
 * @property string $password 密码
 * @property string $rememberMe 是否记住登录状态
 * @author Administrator
 *
 */
class LoginFormA extends CFormModel {
	public $username;
	public $password;
	public $rememberMe = false;
	private $_identity;
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see CModel::rules()
	 */
	public function rules() {
		return array (
				array (
						'username, password', 
						'required' 
				), 
				array (
						'rememberMe', 
						'boolean' 
				) 
		);
	}
	
	/**
	 * 验证
	 */
	public function authenticate() {
		if (! $this->_identity) {
			$this->_identity = new UserIdentity ( $this->username, $this->password );
		}
		if ($this->_identity->authenticate ()) {
			Yii::app ()->user->login ( $this->_identity );
			
			// Yii::app ()->memcache->set ( Yii::app ()->controller->getLoginMKey ( Yii::app ()->user->id ), json_encode ( $this->_identity->user_detail ), 18600 );
		} else {
			$this->addError ( $this->_identity->errorCode, '用户名或密码不正确' );
		}
		return ! $this->hasErrors ();
	}
	
	/**
	 *
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array (
				'username' => '用户名', 
				'password' => '密码' 
		);
	}
}